<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Exchange\AMQPExchangeType;
use PhpAmqpLib\Message\AMQPMessage;

class RunRabbitMQConsumerCmd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rabitmq:consumer_test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'test rabbitmq consumer';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
$params = array(
    'mydlx',
    'mydlq',
    'mydlr',
);  
$connection = new AMQPStreamConnection(env('AMQP_HOST','localhost'), env('AMQP_PORT',5672), env('AMQP_USER','rabbitmq'),env('AMQP_PASS','rabbitmq'));
$channel = $connection->channel();
echo " [*] Waiting for messages. To exit press CTRL+C\n";
$channel->queue_bind($params[1],$params[0]);
$callback = function (AMQPMessage $msg) use($channel) {
    echo ' [x] Received ', $msg->getBody(), "\n";
    $channel->basic_ack($msg->getDeliveryTag());
};
$channel->basic_consume($params[1], '', false, false, false, false, $callback);

try {
    $channel->consume();
} catch (\Throwable $exception) {
    echo $exception->getMessage();
}
        return 0;
    }
}
